<?php

/**
 * Mapper for columns table model
 */
class Default_Model_ColumnsMapper
{
    protected $_db;
    protected $_dbTable;

    protected function initDb($dataBaseName)
    {
        $this->_db = Zend_Db::factory('Pdo_Mysql', array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => 'pass',
            'dbname'   => $dataBaseName
        ));

        return $this;
    }
    
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }

        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }

        $this->_dbTable = $dbTable;

        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Default_Model_DbTable_Columns');
        }
        return $this->_dbTable;
    }

    /**
     * Get collumns from table by table name
     * @param string $tableName
     * @return array
     */
    public function getAllColumnsFromTable($tableName)
    {
        // Quote table nema
        $tableName = $this->getDbTable()->getAdapter()->quote($tableName);

        $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, COLUMN_KEY, EXTRA
            FROM COLUMNS WHERE table_name = ' . $tableName;

        return $this->getDbTable()->getAdapter()->fetchAll($sql);

    }

}